package com.apestech.framework.dictionary;

import com.apestech.framework.cache.Cache;
import com.apestech.framework.db.SqlResultSet;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.db.callback.QueryCallBackHandler;
import com.apestech.framework.dictionary.lib.ReadDictionary;
import com.apestech.framework.dictionary.lib.WriteDictionary;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.FileUtil;

/**
 * @author hup
 * @create 2018-08-31-9:48
 */
public class DataTable extends DictUtil{

    private static String getCacheKey(String tabName){
        return "DICT_TABLE_"+tabName;
    }

    private static String getTableFile(SqlSession session, String tabName){
        return getDictDir(session)+"\\table\\"+tabName;
    }

    public static void updateTableCache(String tabName, Table map){
        Cache.setKey(getCacheKey(tabName), map);
    }

    public static Table getTableFromDB(SqlSession session, String tabName) throws Exception {
        Table map=(Table) Cache.getKey(getCacheKey(tabName));
        //map=null;
        if(map==null){
            byte[] b=session.queryTabCache(tabName);
            //b=null;
            if(b!=null){
                JSONObject t=JSON.toJSON(b);
                if(t!=null){
                    map=new Table(t);
                } else {
                    map=getTableFormDatabase(session, tabName);
                }
            } else {
                map=getTableFormDatabase(session, tabName);
            }
            Cache.setKey(getCacheKey(tabName), map);
        }
        return map;
    }

    public static Table getTable(SqlSession session, String tabName) throws Exception{
        Table map=(Table) Cache.getKey(getCacheKey(tabName));
        if(map==null){
            map=getTableFormDataBase(session, tabName);
            Cache.setKey(getCacheKey(tabName), map);
        }
        return map;
    }

    private static Table getTableFormDatabase(SqlSession session, String tabName) throws Exception{
        WriteDictionary write=new WriteDictionary(session, tabName);
        return write.writeTableToDB();
    }

    private static synchronized Table getTableFormFile(SqlSession session, String tabName) throws Exception {
        String tab=tabName;
        int pos=tabName.indexOf(".");
        if(pos>=0) {
            tab = tabName.substring(pos + 1, tab.length());
        }
        String file=getTableFile(session, tab);
        if(!FileUtil.fileExists(file)) return null;
        ReadDictionary read=new ReadDictionary(getDictDir(session), "table");
        Table map=read.getTable(tab);
        read.close();
        return map;
    }

    private static Table getTableFormDataBase(SqlSession session, String tabName) throws Exception {
        WriteDictionary writer=new WriteDictionary(session, getDictDir(session));
        writer.getTable(tabName);
        return getTableFormFile(session, tabName);
    }

    private static Table getTableFormDataBase1(SqlSession session, String tabName) throws Exception {
        WriteDictionary writer=new WriteDictionary(session, getDictDir(session));
        writer.getTable(tabName);
        return getTableFormFile(session, tabName);
    }

    public static void clearTable(SqlSession session, String tabName) throws Exception {
        Cache.removeKey(getCacheKey(tabName));
        Cache.remeoveKeyLike(tabName);
        FileUtil.deleteDir(getTableFile(session, tabName));
    }

    public static Table refreshTable(SqlSession session, String tabName) throws Exception {
        clearTable(session, tabName);
        return getTable(session, tabName);
    }

}
